package com.sjy.pickphotos.pickphotos.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * recycleview基础适配器
 * Created by sjy on 2017/6/7.
 */

public abstract class CommonAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> implements ICommonAdapter<T, BaseViewHolder> {
    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;

    protected AdapterView.OnItemClickListener onItemClickListener;

    public void setmDatas(List<T> mDatas) {
        if (mDatas == null) {
            mDatas = new ArrayList<>();
        }
        this.mDatas = mDatas;
    }

    public void notifyDataSetChanged(List<T> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

    public List<T> getmDatas() {
        return mDatas;
    }

    public CommonAdapter(Context context, int layoutId, List<T> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
        mDatas = datas;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        convert(holder, mDatas.get(position), position);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        final BaseViewHolder viewHolder = BaseViewHolder.get(mContext, parent, mLayoutId);
        if (onItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(v -> {
                int adapterPosition = viewHolder.getAdapterPosition();
                if (adapterPosition == RecyclerView.NO_POSITION)
                    return;
                onItemClickListener.onItemClick(null, v, adapterPosition, 0);
            });
        }
        return viewHolder;
    }

    public T get(int position) {
        return mDatas.get(position);
    }

    public void add(List<T> datas) {
        int start = 0;
        if (mDatas != null) {
            start = mDatas.size() - 1;
            mDatas.addAll(datas);
        } else {
            mDatas = datas;
        }
        notifyItemRangeChanged(start, datas.size());
    }

    public RecyclerView.Adapter<BaseViewHolder> setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        return this;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    @Override
    public int getItemCount() {
        if (mDatas == null)
            return 0;
        return mDatas.size();
    }

    public abstract void convert(BaseViewHolder holder, T t, int position);


    public void clear() {
        if (mDatas != null) {
            mDatas.clear();
            notifyDataSetChanged();
        }
    }

    public void remove(T t) {
        if (mDatas != null) {
            mDatas.remove(t);
            notifyDataSetChanged();
        }
    }
}
